package com.zjdiepu.www.manage.api.his;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.LoanEditStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.his.IHisYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanEditService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.his.dto.HisYwBusinessLoan;
import com.zjdiepu.www.model.his.ext.HisYwBusinessLoanExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * his_贷款信息历史记录Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年10月18日 11时56分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "HisYwBusinessLoanController", produces="application/json;charset=UTF-8")
@Api(tags = {"his_贷款信息历史记录"}, produces="application/json;charset=UTF-8")
public class HisYwBusinessLoanController extends BaseController {
    @Resource
    private IHisYwBusinessLoanService hisYwBusinessLoanService;
    @Resource
    private IYwBusinessLoanEditService ywBusinessLoanEditService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: pass  <br/>
     * @Description: 审核通过 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "审核通过", response = Boolean.class, httpMethod="POST")
    @RequestMapping(value = "passB", method = RequestMethod.POST)
    public ApiResponse passB(String busiId) {
    	AbstractSysUser currentUser = getCurrentUser();
    	if(StringUtils.isBlank(busiId)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	hisYwBusinessLoanService.saveAuditB(busiId, true, currentUser);
    	return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: refuse  <br/>
     * @Description: 审核不通过 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "审核不通过", response = Boolean.class, httpMethod="POST")
    @RequestMapping(value = "refuseB", method = RequestMethod.POST)
    public ApiResponse refuseB(String busiId) {
    	AbstractSysUser currentUser = getCurrentUser();
    	if(StringUtils.isBlank(busiId)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	hisYwBusinessLoanService.saveAuditB(busiId, false, currentUser);
    	return ApiResponse.success(true);
    }
    /**
     * @author xiongxiaotun <br/>
     * @Title: pass  <br/>
     * @Description: 审核通过 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "审核通过", response = Boolean.class, httpMethod="POST")
    @RequestMapping(value = "pass", method = RequestMethod.POST)
    public ApiResponse pass(String busiId) {
    	AbstractSysUser currentUser = getCurrentUser();
    	if(StringUtils.isBlank(busiId)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	hisYwBusinessLoanService.saveAudit(busiId, true, currentUser);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: refuse  <br/>
     * @Description: 审核不通过 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "审核通过", response = Boolean.class, httpMethod="POST")
    @RequestMapping(value = "refuse", method = RequestMethod.POST)
    public ApiResponse refuse(String busiId) {
    	AbstractSysUser currentUser = getCurrentUser();
    	if(StringUtils.isBlank(busiId)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	hisYwBusinessLoanService.saveAudit(busiId, false, currentUser);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: changeLoanList  <br/>
     * @Description: 修改贷款信息 <br/>
     * @param ywBusinessQueryRequest
     * @param changeLoanType 1:贷款信息修改A 2：贷款信息修改2
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改贷款信息",response = YwBusiness.class, httpMethod="POST")
    @RequestMapping(value = "changeLoanList", method = RequestMethod.POST)
    public ApiResponse changeLoanList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest, @RequestParam(defaultValue="1")Integer changeLoanType){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        if(changeLoanType == 1) {
        	ywBusinessQueryRequest.setLoanEditStatus(LoanEditStatus.YI_SHEN_QING.getCode()); //已申请
        } else if(changeLoanType == 2){
        	ywBusinessQueryRequest.setLoanEditStatus(LoanEditStatus.SHEN_HE_2_SHEN_HE_ZHONG.getCode()); //已申请
        } else if(changeLoanType == 3){
        	ywBusinessQueryRequest.setLoanHis(true);
        }
        Date applyTimeStartDate = ywBusinessQueryRequest.getApplyTimeStartDate();
        if(applyTimeStartDate != null) {
        	ywBusinessQueryRequest.setApplyTimeStart(DateUtil.truncAndAddDays(applyTimeStartDate, 0).getTime() / 1000);
        } 
        Date applyTimeEndDate = ywBusinessQueryRequest.getApplyTimeEndDate();
        if(applyTimeEndDate != null){
        	ywBusinessQueryRequest.setApplyTimeStart(DateUtil.truncAndAddDays(applyTimeEndDate, 1).getTime() / 1000);
        }
        
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增his_贷款信息历史记录 <br/>
     * @param hisYwBusinessLoan
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增his_贷款信息历史记录", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute HisYwBusinessLoan hisYwBusinessLoan) {
    	AbstractSysUser currentUser = getCurrentUser();
    	hisYwBusinessLoan.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	hisYwBusinessLoan.setModifierName(currentUser.getName());
        hisYwBusinessLoanService.saveUpdateAndDelete(hisYwBusinessLoan, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除his_贷款信息历史记录<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除his_贷款信息历史记录", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	hisYwBusinessLoanService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用his_贷款信息历史记录<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用his_贷款信息历史记录", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	hisYwBusinessLoanService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用his_贷款信息历史记录<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用his_贷款信息历史记录", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	hisYwBusinessLoanService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改his_贷款信息历史记录 <br/>
     * @param hisYwBusinessLoan
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改his_贷款信息历史记录", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute HisYwBusinessLoan hisYwBusinessLoan) {
    	AbstractSysUser currentUser = getCurrentUser();
    	hisYwBusinessLoan.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	hisYwBusinessLoan.setModifierName(currentUser.getName());
    	hisYwBusinessLoanService.saveUpdateAndDelete(hisYwBusinessLoan, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询his_贷款信息历史记录<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询his_贷款信息历史记录", response = HisYwBusinessLoan.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       HisYwBusinessLoan hisYwBusinessLoan = hisYwBusinessLoanService.selectById(id);
       if(null == hisYwBusinessLoan) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(hisYwBusinessLoan);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询his_贷款信息历史记录 <br/>
     * @param hisYwBusinessLoanExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询his_贷款信息历史记录",response = HisYwBusinessLoan.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute HisYwBusinessLoanExt hisYwBusinessLoanExt){
        hisYwBusinessLoanExt.notDeleted();
        PageVo page = getPage(hisYwBusinessLoanExt);
        int records = hisYwBusinessLoanService.countByExt(hisYwBusinessLoanExt);
        page.setRecords(records);
        if (records > 0) {
            List<HisYwBusinessLoan> hisYwBusinessLoans = hisYwBusinessLoanService.selectByExt(hisYwBusinessLoanExt);
            page.setRows(hisYwBusinessLoans);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
